package com.deer.wms.system.manage.web;

import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.RedisUtil;
import com.deer.wms.system.manage.model.customer.Customer;
import com.deer.wms.system.manage.model.customer.CustomerCriteria;
import com.deer.wms.system.manage.model.customer.CustomerDto;
import com.deer.wms.system.manage.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author guo
 * @since 2019/11/13
 */
@Api(description = "客户api信息")
@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "新增客户收件地址", notes = "新增客户收件地址")
    @Authority
    @ApiImplicitParams({@ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)})
    @PostMapping("/insert")
    public Result add(@RequestBody CustomerDto customer, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        customerService.createCustomer(customer, currentUser);
        redisUtil.remove("allCustomer");
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        customerService.removeById(id);
        redisUtil.remove("allCustomer");
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Customer customer) {
        List<CustomerDto> list = customerService.findByCustomerCode(customer.getCustomerCode());
        if (list.size() == 0) {
            customerService.updateById(customer);
            redisUtil.remove("allCustomer");
        } else if (list.size() == 1) {
            if (list.get(0).getCustomerId().equals(customer.getCustomerId())) {
                List<CustomerDto> list1 = customerService.findByCustomerName(customer.getCustomerName());
                if (list1.size() == 0) {
                    customerService.updateById(customer);
                    redisUtil.remove("allCustomer");
                } else if (list1.size() == 1 && list1.get(0).getCustomerId().equals(customer.getCustomerId())) {
                    customerService.updateById(customer);
                    redisUtil.remove("allCustomer");
                } else {
                    throw new ServiceException(CommonCode.CUSTOMER_EXISTS, "此客户已存在");
                }

            } else if (!list.get(0).getCustomerId().equals(customer.getCustomerId())) {

                throw new ServiceException(CommonCode.CUSTOMER_CODE_EXISTS, "此客户编码已存在");
            }
        }

        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        Customer customer = customerService.getById(id);
        return ResultGenerator.genSuccessResult(customer);
    }

    @ApiOperation(value = "客户信息", notes = "客户信息")
    @Authority
    @ApiImplicitParams({@ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)})
    @GetMapping("/list")
    public Result list(CustomerCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<CustomerDto> list = customerService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @ApiOperation(value = "客户信息", notes = "客户信息")
    @Authority
    @ApiImplicitParams({@ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)})
    @GetMapping("/allList")
    public Result allList(CustomerCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<CustomerDto> list = (List<CustomerDto>) redisUtil.get("allCustomer");
        if (list == null || list.size() == 0) {
            list = customerService.findList(criteria);
            redisUtil.set("allCustomer", list);
        }

        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
